package com.company.队列;

import java.util.Scanner;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName DataStructure
 * @Description: TODO
 * @date ${DAT}16:36
 */
public class Queue {
    public static void main(String[] args) {
        System.out.println("测试环形数组");
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入队列的大小");
        int size = scanner.nextInt();
        CircleArray circleArray = new CircleArray(size);
        System.out.println("请输入值");
        int n1 = scanner.nextInt();
        circleArray.addQueue(n1);
        System.out.println("请输入值");
        int n2 = scanner.nextInt();
        circleArray.addQueue(n2);
        System.out.println(circleArray.getNum());
        System.out.println(circleArray.getQueue());
        circleArray.showAll();
    }

    static class CircleArray{
        private int maxSize;
        private int front;//默认是0
        private int  rear;//默认是0
        private int[] arr;

        //创建队列构造器
        public CircleArray (int arrMaxSzie){
            maxSize = arrMaxSzie;
            arr = new int[maxSize];
        }

        public boolean isFull(){
            return (rear+1) % maxSize == front;
        }

        public boolean isEmpty(){
            return rear == front;
        }

        public void addQueue(int n){
            if (isFull()){
                System.out.println("队列已满");
                return;
            }
            arr[rear] = n;
            rear = (rear + 1)%maxSize;
        }

        public int getQueue(){
            if (isEmpty()){
                System.out.println("队列为空");
                throw new RuntimeException("队列为空");
            }
            int n = arr[front];
            front = (front+1)%maxSize;
            return n;
        }

        public void showAll(){
            if (isEmpty()){
                System.out.println("队列为空");
                throw new RuntimeException("队列为空");
            }
            for (int i = front; i < front + getNum(); i++){
                System.out.println(arr[i]);
            }
        }

        public int getNum(){
            if (isEmpty()){
                throw new RuntimeException("队列为空");
            }
            return (rear + maxSize - front) % maxSize;
        }



    }
}
